/**
 * Copyright (c) 2012 centeractive ag. All Rights Reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.langcom.xml;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public final class XmlUtils {

	public static String PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";

	private XmlUtils() {
	}

	public static String cutTag(String source, String cutTag, StringBuilder builderBefore, StringBuilder builderAfter) {
		int indexOf = source.indexOf(cutTag);
		if (indexOf == -1) {
			throw new RuntimeException("Not  Found");
		}
		String strBefore = source.substring(0, indexOf);
		builderBefore.append(strBefore);
		source = source.substring(indexOf + cutTag.length(), source.length());
		indexOf = source.indexOf(cutTag);
		String target = source.substring(0, indexOf);
		String strAfter = source.substring(indexOf + cutTag.length(), source.length());
		builderAfter.append(strAfter);
		return target;
	}

	public static Map<String, String> parseResponce(String input) throws ParserConfigurationException, SAXException, IOException {
		Map<String, String> result = new HashMap<String, String>();
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		StringReader reader = new StringReader(input);
		parser.parse(new InputSource(reader), new RegularStructureHandler(result));
		reader.close();
		return result;
	}
}
